// LSL script generated: RLV-New.Object Scripts.DV_MAIN_REMOTH.lslp Sun May  2 00:09:51 Westeuropäische Sommerzeit 2010
list gMenu;
list gMenu2;
string gMenuPrefix = "DV_MAIN_";
key ToucherID;
integer channel_dialog;
integer listen_id;
string msg = "Main Menu for Devices\nVersion 0.1 Beta";
list objectKey = [];
integer gPage;
integer attachment = 1;
string cmdname;
string ping;
key victim = NULL_KEY;
key locker = NULL_KEY;

makeMenu(){
    integer _Scripts = llGetInventoryNumber(10);
    if ((_Scripts == 1)) {
        llOwnerSay("No other Scripts inside");
    }
    else  if ((_Scripts > 1)) {
        integer i = 0;
        (gMenu2 = []);
        while ((i < _Scripts)) {
            string _Name;
            (_Name = llGetInventoryName(10,i));
            if ((llGetSubString(_Name,0,(llStringLength(gMenuPrefix) - 1)) == gMenuPrefix)) {
                (gMenu2 += llGetSubString(_Name,llStringLength(gMenuPrefix),llStringLength(_Name)));
            }
            (i++);
        }
        (gMenu2 += ["Free","Scan","Lock"]);
    }
    llMessageLinked(-4,1338,"device",NULL_KEY);
    (cmdname = ((string)llGetKey()));
}

displayMenu(){
    (channel_dialog = (1 * ((integer)("0x" + llGetSubString(((string)llGetKey()),-5,-1)))));
    (listen_id = llListen(channel_dialog,"",NULL_KEY,""));
    llDialog(ToucherID,msg,gMenu,channel_dialog);
    llSetTimerEvent(45.0);
}

llRlvSay2(string command){
    if ((attachment == 1)) {
        llOwnerSay(("@" + command));
    }
    else  {
        llSay(-1812221819,((((cmdname + ",") + ((string)victim)) + ",") + command));
    }
}

list ListItemReplace(list mylist,string element_old,string element_new){
    integer placeinlist = llListFindList(mylist,[element_old]);
    list newlist = llListReplaceList(mylist,[element_new],placeinlist,placeinlist);
    return newlist;
}

default {

    state_entry() {
        llMessageLinked(-4,1338,"device",NULL_KEY);
        (ping = (("ping," + ((string)llGetKey())) + ",ping,ping"));
        llListen(-1812221819,"",NULL_KEY,ping);
        llSetText("No Victim",<1.0,0.0,1.0>,1);
        makeMenu();
    }

    
    listen(integer channel,string name,key id,string message) {
        if ((channel == -1812221819)) {
            if (((message == ping) && (victim == NULL_KEY))) {
                string pong = (("ping," + ((string)llGetOwnerKey(id))) + ",!pong");
                llSay(-1812221819,pong);
            }
        }
        if ((channel == channel_dialog)) {
            llListenRemove(listen_id);
            llSetTimerEvent(0.0);
            if ((message == "<<")) {
                (gPage--);
                integer maxpages = (llGetListLength(gMenu) / 9);
                if ((gPage < 0)) {
                    (gPage = maxpages);
                }
                displayMenu();
            }
            else  if ((message == ">>")) {
                (gPage++);
                displayMenu();
            }
            else  if ((message == "Lock")) {
                (gMenu2 = ListItemReplace(gMenu2,"Lock","Unlock"));
                (gMenu = gMenu2);
                (locker = ToucherID);
            }
            else  if ((message == "Unlock")) {
                (gMenu2 = ListItemReplace(gMenu2,"Unlock","Lock"));
                (gMenu = gMenu2);
                (locker = NULL_KEY);
            }
            else  if ((message == "Free")) {
                llRlvSay2("!release");
                (victim = NULL_KEY);
                (locker = NULL_KEY);
                llSetText("No Victim",<1.0,0.0,1.0>,1);
                llMessageLinked(-4,1329,((string)victim),ToucherID);
                llMessageLinked(-4,1339,"RESET",ToucherID);
                llListenRemove(listen_id);
                llSetTimerEvent(0.0);
            }
            else  if ((message == "Scan")) {
                llSensor("",NULL_KEY,1,19,6.28318548);
                llListenRemove(listen_id);
                llSetTimerEvent(0.0);
            }
            else  if ((message == "End")) {
                llListenRemove(listen_id);
                llSetTimerEvent(0.0);
            }
            else  if ((~llListFindList(gMenu2,((list)message)))) {
                llListenRemove(listen_id);
                llSetTimerEvent(0.0);
                llMessageLinked(-4,1337,(gMenuPrefix + message),ToucherID);
            }
            else  if ((~llListFindList(gMenu,((list)message)))) {
                (victim = llList2String(objectKey,((integer)message)));
                if ((victim != NULL_KEY));
                string _objectKey = ((string)llGetKey());
                llMessageLinked(-4,1329,((string)victim),ToucherID);
                (msg = (("You have: " + llKey2Name(victim)) + " choosen"));
                llSetText(("Victim:\n" + llKey2Name(victim)),<1.0,0.0,1.0>,1);
                (gMenu = gMenu2);
                if ((locker == NULL_KEY)) {
                    (msg = (("You have: " + llKey2Name(victim)) + " on the Device"));
                    (gMenu = gMenu2);
                    displayMenu();
                }
                else  if ((locker == ToucherID)) {
                    (msg = (("You have: " + llKey2Name(victim)) + " on the Device"));
                    (gMenu = gMenu2);
                    displayMenu();
                }
            }
        }
    }

    
    changed(integer change) {
        if ((change & 1)) {
            makeMenu();
        }
    }

    
    touch_start(integer total_number) {
        (ToucherID = llDetectedKey(0));
        if ((victim == NULL_KEY)) {
            llSensor("",NULL_KEY,1,19,6.28318548);
        }
        else  {
            if ((locker == NULL_KEY)) {
                (msg = (("You have: " + llKey2Name(victim)) + " on the Device"));
                (gMenu = gMenu2);
                displayMenu();
            }
            else  if ((locker == ToucherID)) {
                (msg = (("You have: " + llKey2Name(victim)) + " on the Device"));
                (gMenu = gMenu2);
                displayMenu();
            }
        }
    }

    
    sensor(integer num) {
        integer n;
        (gMenu = []);
        (objectKey = []);
        (msg = "");
        for ((n = 0); (n < num); (++n)) {
            (objectKey += llDetectedKey(n));
            (msg += (((((string)n) + ": ") + llDetectedName(n)) + "\n"));
            (gMenu += ((string)n));
        }
        displayMenu();
    }

    
    link_message(integer sender,integer num,string str,key id) {
        if ((num == 1337)) {
            if ((str == llGetScriptName())) {
                llMessageLinked(-4,1338,llGetScriptName(),NULL_KEY);
                (ToucherID = id);
                if ((victim == NULL_KEY)) {
                    llSensor("",NULL_KEY,1,19,6.28318548);
                }
                else  {
                    (gMenu = gMenu2);
                    displayMenu();
                }
            }
        }
        if ((num == 1338)) {
            if ((str == "attachment")) (attachment = 1);
            else  if ((str == "device")) (attachment = 0);
            else  if ((str != llGetScriptName())) {
                llListenRemove(listen_id);
            }
        }
        if ((num == 1339)) {
            if ((str == "ON_REZ")) {
            }
            if ((str == "RESET")) {
                llRlvSay2("!release");
                (victim = NULL_KEY);
                (locker = NULL_KEY);
                llSetText("No Victim",<1.0,0.0,1.0>,1);
                llMessageLinked(-4,1329,((string)victim),ToucherID);
            }
        }
    }

    
    timer() {
        llListenRemove(listen_id);
        llSetTimerEvent(0.0);
    }
}
